#ifndef _MUNIX_FLASH_H_
#define	_MUNIX_FLASH_H_
/*******************************************************************************
 ** Name: flash.h                                                             **
 ** Description: Declaration of flash stuff.                                  **
 **                                                                           **
 ** Open Source Initiative (OSI) Approved License                             **
 **                                                                           **
 **  Copyright (c) 2009-2010 Barry "Gian" James  <bjames@munixos.net>         **
 **  All rights reserved.                                                     **
 **                                                                           **
 **  Redistribution and use in source and binary forms, with or without       **
 **  modification, are permitted provided that the following conditions are   **
 **  met:                                                                     **
 **                                                                           **
 **  * Redistributions of source code must retain the above copyright notice, **
 **    this list of conditions and the following disclaimer.                  **
 **  * Redistributions in binary form must reproduce the above copyright      **
 **    notice, this list of conditions and the following disclaimer in the    **
 **    documentation and/or other materials provided with the distribution.   **
 **                                                                           **
 **  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS      **
 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED **
 ** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           **
 ** PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT        **
 ** HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,    **
 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED  **
 ** TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR    **
 ** PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    **
 ** LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      **
 ** NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        **
 ** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              **
 **                                                                           **
 ******************************************************************************/
#define _MUNIX_FLASH_H_ID "$Id$"
// Last Modified $DateTime$ by $Author$

#include "common.h"
#include "spi.h"

//////////////////////////[ SD Flash
// R1 Response
#define R1_SUCCESS      0x00
#define R1_IDLE_STATE   0x01
#define R1_ERASE_RESET  0x02
#define R1_ILLEGAL_CMD  0x04
#define R1_CMD_CRC_ERR  0x08
#define R1_ERASE_ERR    0x10
#define R1_ADDR_ERR     0x20
#define R1_PARAM_ERR    0x40

// R2 Response
#define R2_CARD_LOCKED  BIT16(0)
#define R2_LOCK_CMD_FAIL BIT16(1)
#define R2_ERROR        BIT16(2)
#define R2_CC_ERR       BIT16(3)
#define R2_ECC_FAIL     BIT16(4)
#define R2_WP_VIOLATION BIT16(5)
#define R2_ERASE_PARAM  BIT16(6)
#define R2_OUT_OF_RANGE BIT16(7)
#define R2_IN_IDLE      BIT16(8)
#define R2_ERASE_RESET  BIT16(9)
#define R2_ILLEGAL_CMD  BIT16(10)
#define R2_CRC_ERR      BIT16(11)
#define R2_ERASE_ERR    BIT16(12)
#define R2_ADDR_ERR     BIT16(13)
#define R2_PARAM_ERR    BIT16(14)

// OCR bit field definition
#define OCR_LOW_VOLTAGE BIT32(7)
#define OCR_27_28       BIT32(15)
#define OCR_28_29       BIT32(16)
#define OCR_29_30       BIT32(17)
#define OCR_30_31       BIT32(18)
#define OCR_31_32       BIT32(19)
#define OCR_32_33       BIT32(20)
#define OCR_33_34       BIT32(21)
#define OCR_34_35       BIT32(22)
#define OCR_35_36       BIT32(23)
#define OCR_CAP_STATUS  BIT32(30)
#define OCR_PWR_STATUS  BIT32(31)

// Command description
#define CMD_GO_IDLE         0
#define CMD_SEND_OP_COND    1
#define CMD_SWITCH_FUNC     6
#define CMD_SEND_IF_COND    8
#define CMD_SEND_CSD        9
#define CMD_SEND_CID        10
#define CMD_STOP_TX         12
#define CMD_SEND_STATUS     13
#define CMD_SET_BLOCKLEN    16
#define CMD_RD_BLOCK        17
#define CMD_RD_MULT_BLOCK   18
#define CMD_WR_BLOCK        24
#define CMD_WR_MULT_BLOCK   25
#define CMD_PROG_CSD        27
#define CMD_SET_WR_PROT     28
#define CMD_CLR_WR_PROT     29
#define CMD_SEND_WR_PROT    30
#define CMD_ERASE_WR_BLK_START_ADDR 32
#define CMD_ERASE_WR_BLK_END_ADDR   33
#define CMD_ERASE           38
#define CMD_LOCK_UNLOCK     42
#define CMD_APP_CMD         55
#define CMD_GEN_CMD         56
#define CMD_READ_OCR        57
#define CMD_CRC_ON_OFF      59
// Application-specific commands (preceded by CMD_APP_CMD)
#define ACMD_SD_STATUS      13
#define ACMD_SECURITY_RSV1  18
#define ACMD_SEND_NUM_WR_BLKS 22
#define ACMD_SEND_WR_BLK_ERASE_CNT 23
#define ACMD_SECURITY_RSV2  25
#define ACMD_SECURITY_RSV3  26
#define ACMD_SECURITY_RSV4  38
#define ACMD_SD_SEND_OP_COND 41
#define ACMD_SET_CLR_CARD_DETECT 42
#define ACMD_SECURITY_RSV5  43
#define ACMD_SECURITY_RSV5  44
#define ACMD_SECURITY_RSV6  45
#define ACMD_SECURITY_RSV7  46
#define ACMD_SECURITY_RSV8  47
#define ACMD_SECURITY_RSV9  48
#define ACMD_SECURITY_RSV10 49
#define ACMD_SEND_SCR       51


typedef uint8   R1Response;
typedef uint16  R2Response;

struct R3Response
{
    R1Response  r1;
    uint32      ocr;
};

struct R7Response
{
    R1Response  r1;
    uint8       cmdVer:4;
    uint16      reserved;
    uint8       voltAccept:4;
    uint8       echo;
};

// Data Response Tokens
#define DRTOK_DATA_ACCEPTED 0x05    // data accepted
#define DRTOK_DATA_REJ_CRC  0x0B    // data rejected due to crc error
#define DRTOK_DATA_REJ_WR   0x0D    // data rejected due to write error
// Start Block and Tran Tokens
#define SBTOK_BLOCK_WR      0xFE
#define SBTOK_BLOCK_RD      0xFE
#define SBTOK_MULT_RD       0xFE
#define SBTOK_MULT_WR       0xFC
#define TRAN_TOK_STOP       0xFD

// Data Error Token
#define DETOK_ERROR         0x01
#define DETOK_CC_ERR        0x02
#define DETOK_ECC_FAIL      0x04
#define DETOK_OUT_OF_RANGE  0x08

struct FlashCmdFrame
{
    uint8   cmd;
    uint32  arg;
    uint8   crc;
};

//////////////////////////[ On-board DataFlash AT25DF641 64Mbit (8MByte)
#define DF_PAGE_SIZE        256     // bytes
#define DF_SECTOR_SIZE      65535   // bytes
#define DF_SECTOR_NUM       128
#define DF_BLOCK4_SIZE      4096
#define DF_BLOCK32_SIZE     32768
#define DF_BLOCK64_SIZE     65535

// DataFlash command listing
#define DF_CMD_RD_ARRAY     0x1B    // up to 100mhz
#define DF_CMD_RD_ARRAY2    0x0B    // upto 85mhz
#define DF_CMD_RD_ARRAY3    0x03    // up to 50mhz
#define DF_CMD_DO_RD_ARRAY  0x3B    // dual-output
#define DF_CMD_4KBLK_ERASE  0x20
#define DF_CMD_32KBLK_ERASE 0x52
#define DF_CMD_64KBLK_ERASE 0xD8
#define DF_CMD_CHIP_ERASE   0xC7
#define DF_CMD_BYTE_PGM     0x02
#define DF_CMD_PAGE_PGM     0x02
#define DF_CMD_DI_PAGE_PGM  0xA2    // dual-input
#define DF_CMD_SUSP_ERASE   0xB0
#define DF_CMD_CONT_ERASE   0xD0
#define DF_CMD_WREN         0x06
#define DF_CMD_WRDIS        0x04
#define DF_CMD_SECTOR_PROT  0x36
#define DF_CMD_SECTOR_UPROT 0x39
#define DF_CMD_RD_SPR       0x3C
#define DF_CMD_SECTOR_LCKDN 0x33
#define DF_CMD_FREEZE_LCKDN 0x34
#define DF_CMD_RD_SLR       0x35
#define DF_CMD_WR_OTPSR     0x9B
#define DF_CMD_RD_OTPSR     0x77
#define DF_CMD_RDSR         0x05
#define DF_CMD_WRSR1        0x01
#define DF_CMD_WRSR2        0x31
#define DF_CMD_RESET        0xF0
#define DF_CMD_RD_ID        0x9F
#define DF_CMD_PWR_DWN      0xB9
#define DF_CMD_PWR_UP       0xAB

class   FlashController
{
public:

private:
    uint16  blockSize;

    SPI *   dataFlash;
    SPI *   sdFlash;
};

#endif	/* _MUNIX_FLASH_H_ */

